<?php
/**
 * @Name: StaffController.php
 * @Author: yashuai
 */

namespace App\Http\Controllers\Org;

use Illuminate\Support\Facades\DB;
use App\Http\Logic\Org\StaffLogic;
use App\Http\Search\Rbac\UserSearch;
use App\Http\Search\Org\StaffSearch;
use App\Http\Requests\Org\StaffIDRequest;
use App\Http\Requests\Org\StaffSaveRequest;
use App\Http\Requests\Org\StaffSearchRequest;
use App\Http\Requests\Org\StaffRelationNewUserRequest;
use App\Http\Requests\Org\StaffRelationTableUserRequest;

class StaffController
{

    /**
     * @Desc 员工列表
     * @param StaffSearchRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function index(StaffSearchRequest $request)
    {
        $result = StaffSearch::instance($request->getValidationData())->index();
        return _success($result);
    }


    /**
     * @Desc 添加员工
     * @param StaffSaveRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function create(StaffSaveRequest $request)
    {
        $result = StaffLogic::instance($request->getValidationData())->create();
        return _success($result);
    }

    /**
     * @Desc 编辑员工
     * @param StaffSaveRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function update(StaffSaveRequest $request)
    {
        $validator = [
            'StaffID' => 'required|integer'
        ];
        $result    = StaffLogic::instance($request->getValidationData($validator))->update();
        return _success($result);
    }

    /**
     * @Desc 删除员工
     * @param StaffIDRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function delete(StaffIDRequest $request)
    {
        StaffLogic::instance($request->getValidationData())->delete();
        return _success();
    }

    /**
     * @Desc 员工关联账户（创建新的账户）
     * @param StaffRelationNewUserRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function relationNewUser(StaffRelationNewUserRequest $request)
    {
        $data = $request->getValidationData();
        return DB::transaction(function () use ($data) {
            StaffLogic::instance($data)->relationNewUser();
            return _success();
        });
    }

    /**
     * @Desc 员工关联账户（关联账户）
     * @param StaffRelationTableUserRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function relationTableUser(StaffRelationTableUserRequest $request)
    {
        StaffLogic::instance($request->getValidationData())->relationTableUser();
        return _success();
    }

    /**
     * @Desc 获取可关联员工的账户列表
     * @return \Illuminate\Http\JsonResponse
     * @author yashuai
     */
    public function relationUserList()
    {
        $result = UserSearch::instance()->staffRelation();
        return _success($result);
    }

}
